package com.jkt.myself.csv;

public class HealthChecker {
	
	private static long maxheapsize;
	private static long usedheapsize;
	private static long freeheapsize;
	private static long appstartuptimeinmilliseconds;
	private static long lastloggedtimeinmilliseconds;
	
	public static void main(String[] args) {
		System.out.println(checkHealth("Test Tool Health"));
		
	} 
	
	public static String checkHealth(String msg)
	{
		StringBuilder displayString = new StringBuilder();
		
		displayString.append("Message is : "+ msg);
		
		displayString.append("Total Heap Size : " + getMaxheapsize());
		displayString.append("\n");
		displayString.append("Used Heap Size : " + getUsedheapsize());
		displayString.append("\n");
		displayString.append("Free Heap Size : " + getFreeheapsize());
		displayString.append("\n");
		displayString.append("Get Startup Time : " + getAppstartuptimeinmilliseconds());
		displayString.append("\n");
		displayString.append("Get Elapsed Time from last log : " +  getElapsedTimeFromLastLog() );
		displayString.append("\n");
		displayString.append("Get Total Elapsed Time : " + getTotalElapsedTime());
		
		return displayString.toString();
	}

	public static long getTotalElapsedTime()
	{
		return System.currentTimeMillis() - getAppstartuptimeinmilliseconds() ;
	}
	
	public static long getElapsedTimeFromLastLog()
	{
		long newreturntime =0;
		if(lastloggedtimeinmilliseconds==0)
			lastloggedtimeinmilliseconds = System.currentTimeMillis();
		
		newreturntime = lastloggedtimeinmilliseconds - System.currentTimeMillis();
		lastloggedtimeinmilliseconds = System.currentTimeMillis();
		return newreturntime/(1000*60);
	}
	
	
	public static long getMaxheapsize() {
		return (Runtime.getRuntime().maxMemory())/(1024*1024);
	}

	public void setMaxheapsize(long maxheapsize) {
		HealthChecker.maxheapsize = Runtime.getRuntime().maxMemory();
	}

	public static long getUsedheapsize() {
		//return usedheapsize;
		return (Runtime.getRuntime().totalMemory()/(1024*1024));
	}

//	public void setUsedheapsize(long usedheapsize) {
//		HealthChecker.usedheapsize = Runtime.getRuntime().freeMemory();
//	}

	public static long getFreeheapsize() {
		//return freeheapsize;
		return (Runtime.getRuntime().freeMemory())/(1024*1024);
	}

	public void setFreeheapsize(long freeheapsize) {
		HealthChecker.freeheapsize = Runtime.getRuntime().freeMemory();
	}

	public static long getAppstartuptimeinmilliseconds() {
		
		if(appstartuptimeinmilliseconds == 0 )
		{
			appstartuptimeinmilliseconds =  System.currentTimeMillis();	
		}
		
		return appstartuptimeinmilliseconds;
	}

	public void setAppstartuptimeinmilliseconds(long appstartuptimeinmilliseconds) {
		HealthChecker.appstartuptimeinmilliseconds = appstartuptimeinmilliseconds;
	}

	public static long getLastloggedtimeinmilliseconds() {
		return lastloggedtimeinmilliseconds;
	}

	public void setLastloggedtimeinmilliseconds(long lastloggedtimeinmilliseconds) {
		HealthChecker.lastloggedtimeinmilliseconds = lastloggedtimeinmilliseconds;
	}
}
